from . import user_blue
from flask import *
from info.utils import validate_request as vr
from info.models import *
from info.utils.encript import *
from sqlalchemy import and_
from datetime import datetime, timedelta
from info.utils.format_hour_minute import format_hour_minute


'''
基本资料
'''
@user_blue.route('/user/base_info', methods=['GET', 'POST'])
def base_info_handler():
    user: User = User.query.filter(User.id == session.get('user_id')).first()
    if request.method=='GET':
        if user:
            return render_template('news/user_base_info.html', user=user)
        else:
            return vr.error
    else:
        signature = request.form.get('signature')
        nick_name = request.form.get('nick_name')
        gender = request.form.get('gender')
        user.signature = signature
        user.username = nick_name
        user.gender = int(gender)
        try:
            db.session.add(user)
            db.session.commit()
        except:
            # 刷新，不然报错=======就这么用，不懂为啥
            db.session.merge(user)
            db.session.refresh(user)
            # ======================================
            db.session.rollback()
            return vr.failure
        return vr.sucess

'''
密码修改
'''
@user_blue.route('/user/pass_info', methods=['GET', 'POST'])
def pass_info_handler():
    if request.method == 'GET':
        return render_template('news/user_pass_info.html')
    else:
        user = User.query.filter(User.id == session.get('user_id')).first()
        if not user:
            return vr.error

        old_password = request.form.get('old_password')
        print(old_password)
        new_password = request.form.get('new_password')
        re_password = request.form.get('new_password2')
        pass_error = vr.v_new_old_password(user.password,new_password,old_password,re_password)
        if pass_error:
            return pass_error

        user.password = md5(new_password)
        try:
            db.session.add(user)
            db.session.commit()
        except:
            return vr.error
        return vr.sucess
'''
我的关注
'''
@user_blue.route('/user/user_follow')
def user_follow_handler():
    user = User.query.filter(User.id == session.get('user_id')).first()

    current_page = request.args.get('page')
    if not current_page:
        current_page = 1

    paginate_ = user.query.paginate(int(current_page), 4)
    follow_s = paginate_.items
    # if user in follow_s:
    #     follow_s.remove(user)

    total_page = paginate_.pages
    if user:
        return render_template('news/user_follow.html', user=user,follow_s = follow_s,
                               total_page=total_page,current_page=current_page
                               )
    else:
        return vr.error

"""
关注
"""
@user_blue.route('/user/add_follow')
def add_follow_handler():
    user = User.query.filter(User.id == session.get('user_id')).first()
    follow_id = request.args.get('follow_id')
    follow_user = User.query.filter(User.id == follow_id).first()
    try:
        user.concern_s.append(follow_user)
        db.session.commit()
    except:
        return vr.failure
    return vr.sucess
"""
取消关注
"""
@user_blue.route('/user/cancel_follow')
def cancel_follow_handler():
    user = User.query.filter(User.id == session.get('user_id')).first()
    follow_id = request.args.get('follow_id')
    follow_user = User.query.filter(User.id == follow_id).first()
    # try:
    user.concern_s.remove(follow_user)
    db.session.commit()
    # except:
    # return vr.failure
    return vr.sucess